package main.java.tmi.common.web;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.service.UserService;
import main.java.tmi.common.service.UserVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class LoginController {
	
	@Resource(name = "userServiceImpl")
	private UserService userService;

    /**
     * The Constructor
     */
    public LoginController() {
        //logger = LogFactory.getLog(this.getClass());
    }
    
	/**
	 * login
	 * 
	 * @param ModelMap model
	 * @param loginId 
	 * @param userPassword 
	 * @param HttpServletRequest request
	 * @param HttpServletResponse response
	 * @return : 
	 * @exception Exception
	 */
	@RequestMapping(value = "/common/login/login")
	public ModelAndView login(@RequestParam Map param, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String loginId = param.get("loginId").toString();
		String userPassword = param.get("userPassword").toString();
		System.out.println("====================================LoginController login start=======================================");
		Map<String, Object> hmap = new HashMap<String, Object>();
		int nResult = -1;
		String serverName = request.getServerName();
		if (loginId != null && userPassword != null) {
			System.out.println("loginId---------------------------->"+loginId);
			System.out.println("userPassword---------------------------->"+userPassword);
			System.out.println("serverName---------------------------->"+serverName);
			nResult = userService.login(loginId, userPassword, request, response);
			
		}
		
		if(nResult != -1){
			UserVO userInfo = (UserVO) RequestContextHolder.getRequestAttributes().getAttribute("userInfo", RequestAttributes.SCOPE_SESSION);
			System.out.println("userVO.getUserId()-------------->"+userInfo.getUserId());
	    	System.out.println("userVO.getUserNm()-------------->"+userInfo.getUserName());
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("nResult",nResult);
		mav.setViewName("/index");
		return mav;
	}
	
	/**
	 * logout
	 * 
	 * @param HttpServletRequest request
	 * @param HttpServletResponse response
	 * @return 
	 * @exception Exception
	 */
	@RequestMapping(value = "/common/login/logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		userService.logout(request, response);
		return "/index";
	}
	
	/**
	 * index
	 * 
	 * @param HttpServletRequest request
	 * @param HttpServletResponse response
	 * @return 
	 * @exception Exception
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {	
		return "/index";
	}
	
	/**
	 * redirectLogin
	 * 
	 * @param HttpServletRequest request
	 * @param HttpServletResponse response
	 * @return 
	 * @exception Exception
	 */
	@RequestMapping(value = "/redirectLogin")
	public String redirectLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {	
		return "/tmi/common/redirectLogin";
	}
	
	/**
	 * redirectLogin
	 * 
	 * @param HttpServletRequest request
	 * @param HttpServletResponse response
	 * @return 
	 * @exception Exception
	 */
	@RequestMapping(value = "/mainPop")
	public String mainPop(HttpServletRequest request,
			HttpServletResponse response) throws Exception {	
		return "/tmi/popup/mainPop";
	}
}
